package com.yxw.yxnet_cd_center.gateway.common;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: lx
 * @Date: 2023.10.20
 */
@RefreshScope
@Service
public class UrlResolver {

    /**
     * 从get请求参数校验token
     */
    private final static List<String> VERIFY_TOKEN_URL_LIST_FROM_QUERY_PARAMS = new ArrayList<>();


    /**
     * 需要token，但是不校验过期的集合
     *
     * @return
     */
    public static boolean checkTokenExpire(String requestPath) {
        for (String i : UrlWhileList.getTokenExpireList()) {
            if (requestPath.contains(i)) {
                return false;
            }
        }
        return true;
    }

    @PostConstruct
    public void initVerifyTokenGetUrl() {
        VERIFY_TOKEN_URL_LIST_FROM_QUERY_PARAMS.add("/payment/api/h5_pay/v1/pre_create_pay_order");
    }

    /**
     * 验证url是需要获取get请求参数token校验
     *
     * @param requestPath
     * @return
     */
    public static boolean checkTokenVerifyFromGetRequestParams(String requestPath) {
        for (String i : VERIFY_TOKEN_URL_LIST_FROM_QUERY_PARAMS) {
            if (requestPath.contains(i)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 验证url是否匹配,支持精确匹配和模糊匹配
     *
     * @param requestPath
     * @return
     */
    public static boolean check(String requestPath) {
        for (String i : UrlWhileList.getUrlList()) {
            if (requestPath.contains(i)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 校验该url是否需要权限控制
     *
     * @param requestPath
     * @return
     */
    public static boolean checkPermission(String requestPath) {
        if (CollectionUtil.isNotEmpty(UrlPermissionList.getUrlList())) {
            for (String url : UrlPermissionList.getUrlList()) {
                if (requestPath.equals(url)) {
                    return true;
                }
            }
        }
        return false;
    }
}
